﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Core.Dtos;
using Core.Messages;
using Core.Queries;
using Core.IRepository;
using Core.Domain;
using Library.Helpers;
using Front.Controllers.Api;
using System.IO;
using System.Collections;
using Core.IServices;
using Library.Logging;
using Core.Factories;
using Core.Context;
using System.Web.Http.Cors;
using Share.Enterprise.Api;

namespace Front.Areas.Admin.Controllers.Api
{
    [RoutePrefix("api/admin/settings")]
    //[Authorize(Roles = "Admin")]
    [EnableCors(origins: "*", headers: "*", methods: "*")]
    public class SettingApiController : ApiController, ICrudApiController<int, SettingDto, SettingQuery>, IQueryApiController<int, SettingDto, SettingQuery>
    {
        private readonly IManageSettingService service;
        private readonly IReadSettingService settingCacheService;
        private readonly IRequestFactory requestFactory;
        private readonly ICurrentContext currentContext;

        public SettingApiController(IManageSettingService service, IReadSettingService settingCacheService, ILogger logger, ICurrentContext currentContext, IRequestFactory requestFactory)           
        {
            this.service = service;
            this.settingCacheService = settingCacheService;
            this.requestFactory = requestFactory;
            this.currentContext = currentContext;
        }

        [HttpPost]
        [Route("searches")]
        public QueryResponse<SettingDto> FindBy(SettingQuery query)
        {
            Request<SettingQuery> request = requestFactory.GetInstance<SettingQuery>(query);
            return service.FindBy(request);
        }

        [Route("{id}")]
        public SettingDto Get(int id)
        {
            return service.FindByID(id);
        }

        [HttpPost]
        [Route("")]
        public virtual Response<int> Post(SettingDto dto)
        {
            Request<SettingDto> request = requestFactory.GetInstance<SettingDto>(dto);
            return service.Add(request);
        }

        [HttpPut]
        [Route("{id}")]
        public virtual Response Put(int id, SettingDto dto)
        {
            Request<SettingDto> request = requestFactory.GetInstance<SettingDto>(dto);
            return service.Edit(id, request);
        }

        [HttpDelete]
        [Route("{id}")]
        public virtual Response Delete(int id)
        {
            return service.Delete(id);
        }

        [HttpDelete]
        [Route("cache")]
        public Response DeleteCache()
        {
            return settingCacheService.ResetStaticCache();
        }
    }
}